/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import Adaptor.JLocationListValue;
import Interface.IFile;
import Interface.ListAction;
import InterfaceClient.JDriveCombo;

import Packets.pkValue;
import StaticClass.ArrSocket;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import org.w3c.dom.events.MouseEvent;

/**
 *
 * @author Oho
 */
public class ThreadFile_Tree extends Thread {

    Socket clientSocket;

    ThreadFile_Tree(Socket s) {
        //try {
        //   ArrSocket.ArrFileTree.add(this);
        clientSocket = s;
        temp = this;

    }
    public ThreadFile_Tree temp;
    //JDriveCombo cbb =null;
    public JDriveCombo name1;
    public JLocationListValue namefile;
    public JLocationListValue namefd;
    String value = "";
    int il = 0;
    IFile ifile;
    DefaultComboBoxModel model = null;
    //int click = 0;

    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if (inp == null) {
            inp = new ObjectInputStream(clientSocket.getInputStream());
        }
        if (model == null) {
            model = (DefaultComboBoxModel) inp.readObject();
        }

        //  name1 = (JDriveCombo) inp.readObject();
        ArrSocket.arrIconFile = (ArrayList<JLabel>) inp.readObject();
        
        System.out.println("nhan thanh cong ds file");
        ArrSocket.arrIconForder = (ArrayList<JLabel>) inp.readObject();
       
        //        for(int i =0;i<ArrSocket.arrIconForder.)
        System.out.println("nhan thanh cong ds forder ");

        namefile = (JLocationListValue) inp.readObject();

        namefd = (JLocationListValue) inp.readObject();
        ArrSocket.jListFile = namefile;
        ArrSocket.jListFoder = namefd;

        Action displayAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                try {
                    JLocationListValue list = (JLocationListValue) e.getSource();
                    String str = list.getSelectedFile();
                    System.out.println(str);
                    pkValue pk = new pkValue(1, 1, str);
                    outp.writeObject(pk);
                    outp.flush();
                } catch (IOException ex) {
                    Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
                }



            }
        };

        ListAction la = new ListAction(namefile, displayAction);
        la = new ListAction(namefd, displayAction);

        ifile.setjList2Files(namefile);
        ifile.setjList2Folder(namefd);
        ifile.cbbClient.setModel(model);





    }

    @Override
    public void run() {
        ArrSocket.ArrFileTree.add(this);
        while (true) {
            try {
                ThreadRead();

            } catch (FileNotFoundException ex) {
                Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    ObjectOutputStream outp = null;
    ObjectInputStream inp = null;

    public void ThreadWriteTree(String tree, IFile ifi) {
        try {
            this.ifile = ifi;
            if (outp == null) {
                try {
                    outp = new ObjectOutputStream(clientSocket.getOutputStream());
                } catch (IOException ex) {
                    Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            pkValue pk = new pkValue();
            pk.setCommand(2);
            pk.setValue(1);
            pk.setObj(tree);
            outp.writeObject(pk);
            outp.flush();
        } catch (IOException ex) {
            Logger.getLogger(ThreadFile_Tree.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
